<?php

namespace app\admin\controller;

use app\shop\model\ShopGoods;
use app\shop\model\ShopGoodsCate;
use app\shop\model\ShopGoodsModule;
use app\shop\model\ShopGoodsSpec;
use app\shop\model\ShopGoodsSpecs;
use app\shop\model\ShopGoodsStock;
use app\shop\model\ShopGoodsTag;
use app\common\service\Status;
use app\shop\model\ShopOrder;
use app\shop\model\ShopSeckillGood;
use app\shop\model\ShopSpellGood;
use buildView\Actions;
use buildView\Button;
use buildView\Filter;
use buildView\Form;
use buildView\Grid;
use library\Controller;
use library\tools\Data;
use think\Db;
use think\db\Query;
use app\common\model\ShopStore;


class StoreGoods extends Controller
{
    public function index($store_id)
    {
        $grid = new Grid(new ShopGoods());
        $storeData = ShopStore::where('id',$store_id)->find();
        $grid->setTitle($storeData['name'].'--商品管理');
        $grid->model()->withSum(['orderGood' => function ($q) {
            $q->where('status',1);
            return 'sale_num';
        }], 'num')->where('sid',$store_id);
        $grid->logo('商品图片')->width(150)->image(5,80,80);
        $grid->column('good_name', '商品名称')->display(function ($val, $data) {
            $html = "{$data['title']}";
            return $html;
        })->width(200)->align('left');
        $grid->column('cate_info', '所属分类')->display(function ($val, $data) {
            $html = "{$data['cate']['title']}";
            return $html;
        })->width(200)->align('left');
        $grid->column('sale_num', '销量排序')->display(function ($val, $data) {
            $sale = $data['sale_num'] ? $data['sale_num']:0;
            return '销量：<span class="layui-badge layui-bg-green">'. $sale .'</span>';
        })->width(150)->align('left')->sort();
        $grid->column('price', '价格')->display(function ($val, $data) {
            return '￥' . $data['price'];
        })->width(150)->align('left');
        
        $grid->column('vip_price', '会员价格')->display(function ($val, $data) {
            return '￥' . $data['vip_price'];
            
        })->width(150)->align('left');
        $grid->column('status', '商品状态')->width(150)->align('left')->switchs([
            'on' => ['text' => '上架', 'value' => 1],
            'off' => ['text' => '下架', 'value' => 0],
        ]);
        $grid->actions(function (Actions $action, $data) {
            $action->hideDetail();
            $action->width(200);
        });
        $grid->filter(function (Filter $filter) {
            $filter->like('title', '商品名称');
            $cates = ShopGoodsCate::field('id,title as text,pid')->where('pid', 0)->with(['son' => function ($q) {
                $q->field('id,title as text,pid');
            }])->select();
            $filter->eq('cate_id','商品分类')->select($cates);
            $filter->eq('status', '商品状态')->select(Status::SHOPSTATUS());
        });
        // 隐藏导出按钮
        $grid->hideExportButton();
        // 设置添加按钮参数
        $grid->setAddButtonParam(['store_id'=>$store_id]);
        // 设置编辑按钮参数
        $grid->setEditButtonParam(['store_id'=>$store_id]);
        return $grid->view();
    }

    public function form($store_id)
    {
        $form = new Form(new ShopGoods());
        $storeData = ShopStore::where('id',$store_id)->find();
        $form->setTitle($storeData['name']);
        // 设置方框风格的表单集
        $form->setThemePane();
        // 隐藏框框 默认sid = $store_id
        $form->hidden('sid', '门店id')->default($store_id);
        // select选择框 数据
        $cates = ShopGoodsCate::field('id,title')->where('pid','>', 0)->where('sid',$store_id)->where('status',1)->where('is_deleted',0)->column('title','id');
        
        $form->text('title', '商品名称')->required();
        $form->select('cate_id', '商品分类')->options($cates)->default(1);
        $form->number('vip_price', '会员价格')->min(1)->required();
        $form->number('price', '商品价格')->min(1)->layui('inline')->md(4)->required();
        $form->switch('is_bed','是否为床品')->swithcStates([
            'off'=>['text'=>'否','value'=>0],
            'on'=>['text'=>'是','value'=>1],
        ])->help('用于下单时扣除会员卡床品的次数');
        $form->image('logo', '商品图片')->layui('block text-center')->md(1)->help('建议图片尺寸200 * 80')->required();
        return $form->view();
    }
}
